//
//  Set.m
//  MUBATE
//
//  Created by sunbin on 2017/2/27.
//  Copyright © 2017年 Quanli. All rights reserved.
//

#import "Set.h"
#import "CacheSize.h"
#import "ExitApp.h"
#import "LoadHtml.h"

#import "AboutUsViewController.h"
#import "Messageswitch.h"
@interface Set ()<UITableViewDataSource ,UITableViewDelegate, LCActionSheetDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableview;

@end

@implementation Set
{
    NSArray *cellDefs;
}

- (void)viewDidLoad {
    [super viewDidLoad];


    [self initView];[self loadData];
    
}


- (void)initView{
    self.tableview.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
}

- (void)loadData{
    cellDefs = @[
                 @[NSLocalizedString(@"newmessagereminder", @"A new message"),
                   NSLocalizedString(@"about"  , @"About us"),
                   NSLocalizedString(@"terms"  , @"Terms of service"),
                   NSLocalizedString(@"Privacy", @"Privacy policy"),
                   NSLocalizedString(@"clear"  , @"Clear the cache")],
                 @[NSLocalizedString(@"cancellation.amount" , @"Log out")],
                 ];

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return cellDefs.count;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [[cellDefs objectAtIndex:section] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellID = @"cellID";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellID];
        
    }
    NSArray *itemAry = [cellDefs objectAtIndex:indexPath.section];
    cell.textLabel.text = [itemAry objectAtIndex:indexPath.item];

    if (indexPath.section == 0){
        if (indexPath.row == 4) {
            NSString *size = [[CacheSize sharedCacheSize] cacheSize];
            cell.detailTextLabel.text = size;
        }else{
            if (indexPath.row !=0 && indexPath.row != 1) {
                cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            }
        }}
    
    return cell;

}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];    
    NSInteger section = indexPath.section;
    if (section == 0) {
        NSInteger row = indexPath.row;
        if (row !=4) {
            if (row >1) {
                LoadHtml *loadHtml = [[LoadHtml alloc] initWithNibName:@"LoadHtml" bundle:nil];
                loadHtml.index = row - 2;
                [self.navigationController pushViewController:loadHtml animated:YES];

            }else{
                NSArray *ctlAry = @[[[Messageswitch alloc] initWithNibName:@"Messageswitch" bundle:nil],
                                    [[AboutUsViewController alloc] initWithNibName:@"AboutUsViewController" bundle:nil],
                                    ];
                [self.navigationController pushViewController:ctlAry[row] animated:YES];                
            }
        }else{
            [self clear];
            return;
        }
    }
    if (section == 1) {
        [self logout];
        return;
        
    }
}


// 退出
- (void)logout{
    NSArray *logoutType = @[NSLocalizedString(@"exit", @"Exit") ,NSLocalizedString(@"close", @"Close")];
    LCActionSheet *sheet = [[LCActionSheet alloc] initWithTitle:NSLocalizedString(@"Are you sure you want to write off?", @"Are you sure you want to write off?")
                                                   buttonTitles:logoutType
                                                 redButtonIndex:-1
                                                       delegate:self];
    [sheet show];
    
}

#pragma mark - LCActionSheet 代理方法
- (void)actionSheet:(LCActionSheet *)actionSheet didClickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 0) {
        [[NSNotificationCenter defaultCenter] postNotificationName:LOGOUT object:nil];
        return;
    }
    if (buttonIndex == 1) {
        [[ExitApp sharedExitApp] exit];
        return;
    }
    NSLog(@"> Clicked Index: %ld", (long)buttonIndex);
}

// 清理缓存
- (void)clear{
    WEAKSELF;
    [NHUtils clear];
    [self.navigationController.view showLoading:@"正在清理缓存"];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [weakSelf.navigationController.view hideHUD];
        [weakSelf.navigationController.view showSuccess:@"清理成功"];
        [weakSelf.tableview reloadData];
        NSLog(@"%@" ,[[CacheSize sharedCacheSize] cacheSize]);
        
        // 发送通知刷新
        [[NSNotificationCenter defaultCenter] postNotificationName:CLEARCACHE object:nil];
    });
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return kscaleHeight(50.f);
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)])
    {[cell setSeparatorInset:UIEdgeInsetsMake(0, 15, 0, 15)];
    }
    if ([cell respondsToSelector:@selector(setLayoutMargins:)])
    {[cell setLayoutMargins:UIEdgeInsetsMake(0, 15, 0, 15)];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return kscaleHeight(10.f);
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section == 0) { return kscaleHeight(30.f);
    }else{return kscaleHeight(0.f);
    }
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}


@end
